package com.axend.aerosense.base.viewmodel;

import com.axend.aerosense.base.bean.BaseBean;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;

import java.lang.ref.WeakReference;

public abstract class CustomBaseViewModel<T extends BaseBean> extends BaseViewModel{
    private WeakReference<ILoadDataResult<T>> loadDataReference = null;
    public boolean isRefresh = false;

    public CustomBaseViewModel(IBaseView baseViewImpl, ILoadDataResult<T> loadDataResultImpl){
        baseViewWeakReference = new WeakReference<>(baseViewImpl);
        loadDataReference = new WeakReference<>(loadDataResultImpl);
//        init();
    }

    public WeakReference<ILoadDataResult<T>> getLoadDataReference() {
        return loadDataReference;
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        loadDataReference = null;
    }

    public void loadDataSuccess(T baseBean) {
        if(!isRefresh && null != baseViewWeakReference && baseViewWeakReference.get() != null){
            baseViewWeakReference.get().showContent();
        }
        if(null != loadDataReference && loadDataReference.get() != null){
            loadDataReference.get().loadDataSuccess(baseBean);
        }
    }

    public void loadDataFail(String desc) {
        if(!isRefresh && null != baseViewWeakReference && baseViewWeakReference.get() != null){
            baseViewWeakReference.get().showFailure(desc);
        }
        if(null != loadDataReference && loadDataReference.get() != null){
            loadDataReference.get().loadDataFail(desc);
        }
    }
}
